using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Business.Entities.Rules;

namespace OR.Lazarus.Business.Entities.Rules.Lazarus
{
	/// <summary>
	/// Defines a set of rules for a particular entity type.  This allows different naming conventions and settings for Tables vs. OOP Objects, etc.
	/// </summary>
   public class RuleDetail : EntityBase
   {
		protected System.String mName;
		protected System.String mType;
		protected string mDescription;
	
		protected Derived.NamingRuleDetail mNamingRule;
		protected Derived.PKRuleDetail mPKRule;
		protected Derived.FKRuleDetail mFKRule;

		public RuleDetail()
		{
	
			mNamingRule = new Derived.NamingRuleDetail();
			mNamingRule.ParentEntity = this;
			mPKRule = new Derived.PKRuleDetail();
			mPKRule.ParentEntity = this;
			mFKRule = new Derived.FKRuleDetail();
			mFKRule.ParentEntity = this;
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public System.String Type
		{
			get
			{
				return ( mType );
			}
			set
			{
				mType = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Derived.NamingRuleDetail NamingRule
		{
			get
			{
				return ( mNamingRule );
			}
			set
			{
				mNamingRule = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Derived.PKRuleDetail PKRule
		{
			get
			{
				return ( mPKRule );
			}
			set
			{
				mPKRule = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Derived.FKRuleDetail FKRule
		{
			get
			{
				return ( mFKRule );
			}
			set
			{
				mFKRule = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mType = DataUtility.GetString( pSource, "type" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lcz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			

			// FK Values
			XmlElement vNamingRule = ( XmlElement )pSource.SelectSingleNode( "lcz:naming-rule", PersistenceConfig.Namespaces );
			
			if ( vNamingRule != null )
			{
				mNamingRule.LoadXml( vNamingRule );
			}
			XmlElement vPKRule = ( XmlElement )pSource.SelectSingleNode( "lcz:pk-rule", PersistenceConfig.Namespaces );
			
			if ( vPKRule != null )
			{
				mPKRule.LoadXml( vPKRule );
			}
			XmlElement vFKRule = ( XmlElement )pSource.SelectSingleNode( "lcz:fk-rule", PersistenceConfig.Namespaces );
			
			if ( vFKRule != null )
			{
				mFKRule.LoadXml( vFKRule );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lcz", "Rule", "http://schemas.icitadel.com/Lazarus/Rules.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mType ) )
			{
				vNewElement.SetAttribute( "type", mType );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lcz", "description", "http://schemas.icitadel.com/Lazarus/Rules.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// FK Values
			vNewElement.AppendChild( mNamingRule.SaveXml( p_docContext ) );
			
			vNewElement.AppendChild( mPKRule.SaveXml( p_docContext ) );
			
			vNewElement.AppendChild( mFKRule.SaveXml( p_docContext ) );
			

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.RuleDetail v_oReturn = new Derived.RuleDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Type = mType;
			v_oReturn.Description = mDescription;
			v_oReturn.NamingRule = ( Derived.NamingRuleDetail )mNamingRule.Clone();
			v_oReturn.PKRule = ( Derived.PKRuleDetail )mPKRule.Clone();
			v_oReturn.FKRule = ( Derived.FKRuleDetail )mFKRule.Clone();
			
			// FK Values
			v_oReturn.NamingRule = this.NamingRule.CloneTyped();
			
			v_oReturn.PKRule = this.PKRule.CloneTyped();
			
			v_oReturn.FKRule = this.FKRule.CloneTyped();
			

			// Collections
			
			return ( v_oReturn );
		}

		public virtual Derived.RuleDetail CloneTyped()
		{
			return ( ( Derived.RuleDetail )this.Clone() );
		}
   }
}
   